package com.atlantis.apps.notifier.service;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.util.Log;

import com.atlantis.apps.notifier.util.SNConstants;
import com.atlantis.apps.notifier.util.SNUtility;

/**
 */
public class SNBackgroundService extends Service {
	public static final String ACTION_START = "service.start";
	public static final String ACTION_SUBMIT = "service.submit";
	public static final String ACTION_STOP = "service.stop";
	private Timer timer = new Timer();

	// private NotificationManager mNM;

	@Override
	public void onCreate() {
		super.onCreate();
	}

	@Override
	public void onDestroy() {
		if (timer != null)
			timer.cancel();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		SharedPreferences mySharedPreferences = getSharedPreferences(SNConstants.SNSETTINGS, Activity.MODE_PRIVATE);

		if (mySharedPreferences != null) {
			int refreshInterval = Integer.parseInt(mySharedPreferences.getString(SNConstants.SNREFRESH, "15"));

			timer.scheduleAtFixedRate(new TimerTask() {
				public void run() {
					_updateView();
				}
			}, 0, refreshInterval * (60000));
			Log.i(getClass().getSimpleName(), "Timer started!!!");
		}

		return START_STICKY;
	}

	private void _updateView() {
		try {
			SNUtility.updateSongView(this.getBaseContext());
		} catch (Exception e) {
			Log.d(getBaseContext().getClass().getSimpleName(), "Check your Settings!");
		}
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
}
